package com.xm.xmapp;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import android.os.Bundle;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v4.widget.SwipeRefreshLayout.OnRefreshListener;
import android.view.View;
import android.widget.ListView;

import com.alibaba.fastjson.JSON;
import com.android.volley.VolleyError;
import com.xm.xmapp.adapter.AccountRecordRefundAdapter;
import com.xm.xmapp.app.MyAppcation;
import com.xm.xmapp.base.BaseActivity;
import com.xm.xmapp.customview.CustomLayoutText;
import com.xm.xmapp.myinterface.DisssmissInterFace;
import com.xm.xmapp.myinterface.GetDataInterFace;
import com.xm.xmapp.request.bean.RequestRefundDetails;
import com.xm.xmapp.response.bean.ResponseAccountRecord;
import com.xm.xmapp.response.bean.ResponseRefundDetails;

public class AccountRecordDetailRefundActivity extends BaseActivity implements OnRefreshListener {

	private ResponseAccountRecord bean;
	private CustomLayoutText cl_order_sn, cl_add_time, cl_opeart_type, cl_remark;
	private ListView lv_list;
	private AccountRecordRefundAdapter mAdapter;
	private ArrayList<ResponseRefundDetails> list;
	private SwipeRefreshLayout id_swipe_ly;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_account_record_detail);
	}

	@Override
	public void initView() {

		cl_order_sn = (CustomLayoutText) findViewById(R.id.cl_order_sn);
		cl_add_time = (CustomLayoutText) findViewById(R.id.cl_add_time);
		cl_opeart_type = (CustomLayoutText) findViewById(R.id.cl_opeart_type);
		cl_remark = (CustomLayoutText) findViewById(R.id.cl_remark);
		lv_list = (ListView) findViewById(R.id.lv_list);
		id_swipe_ly = (SwipeRefreshLayout) findViewById(R.id.id_swipe_ly);
		id_swipe_ly.setOnRefreshListener(this);
	}

	@Override
	public void initData() {

		setMyTitle("账单详情");
		bean = (ResponseAccountRecord) getIntent().getSerializableExtra("bean");
		cl_order_sn.setText2(bean.order_sn);
		SimpleDateFormat formatter = new SimpleDateFormat("yyyy年MM月dd日   HH:mm:ss     ");
		Date curDate = new Date(bean.add_time * 1000);
		// 获取当前时间
		String str = formatter.format(curDate);
		cl_add_time.setText2(str);
		cl_opeart_type.setText2(bean.money_user_way_text);

		if (bean.remark == null || bean.remark.equals("")) {
			cl_remark.setVisibility(View.GONE);
		} else {
			cl_remark.setText2(bean.remark);
		}
		list = new ArrayList<ResponseRefundDetails>();
		mAdapter = new AccountRecordRefundAdapter(list, this, R.layout.item_account_record_details);
		lv_list.setAdapter(mAdapter);
		getData();
	}

	private void getData() {
		RequestRefundDetails requestBean = new RequestRefundDetails();// 4225
		requestBean.account_record_id = bean.account_record_id;
		promptDialog.show();
		MyAppcation.getMyAppcation().getPostData(this, requestBean, new GetDataInterFace<String>() {

			@Override
			public void onResponse(String response) {
				promptDialog.dismiss();
				id_swipe_ly.setRefreshing(false);
				List<ResponseRefundDetails> mList = JSON.parseArray(response, ResponseRefundDetails.class);

				ArrayList<ResponseRefundDetails> mListSuccess = new ArrayList<ResponseRefundDetails>();
				ArrayList<ResponseRefundDetails> mListFail = new ArrayList<ResponseRefundDetails>();
				for (ResponseRefundDetails responseRefundDetails : mList) {
					if (responseRefundDetails.handle == 1) {
						mListSuccess.add(responseRefundDetails);
					} else {
						mListFail.add(responseRefundDetails);
					}
				}
				list.clear();
				list.addAll(mListSuccess);
				list.addAll(mListFail);

				int temp = -1;
				for (int i = 0, size = list.size(); i < size; i++) {
					if (list.get(i).handle == 2) {
						temp = i;
						break;
					}
				}
				mAdapter.setFirstPosition(temp);
				mAdapter.notifyDataSetChanged();

			}
		}, new DisssmissInterFace() {

			@Override
			public void putError(VolleyError error) {
				promptDialog.dismiss();
				id_swipe_ly.setRefreshing(false);
			}
		});
	}

	@Override
	public void onRefresh() {
		// getData();
		id_swipe_ly.setRefreshing(false);
	}

	@Override
	public void initEvent() {
	
	}
}
